package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.domain.OrderReceiveInfo;
import com.srwl.mytx.R;
import com.srwl.mytx.widget.ConfirmDialog;

import java.util.List;

public class OrderReceiveInfoAdapter extends RecyclerView.Adapter<OrderReceiveInfoAdapter.OrderReceiveInfoViewHolder> {

    private final Context context;
    private List<OrderReceiveInfo> orderReceiveInfoList;
    private InteractionListener listener;

    public OrderReceiveInfoAdapter(Context context, List<OrderReceiveInfo> orderReceiveInfoList, InteractionListener listener) {
        this.context = context;
        this.listener = listener;
        this.orderReceiveInfoList = orderReceiveInfoList;

    }

    @NonNull
    @Override
    public OrderReceiveInfoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.order_received_info_item_layout, viewGroup, false);
        return new OrderReceiveInfoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderReceiveInfoViewHolder viewHolder, int position) {
        OrderReceiveInfo orderReceiveInfo = orderReceiveInfoList.get(position);
        viewHolder.tv_contact.setText(orderReceiveInfo.getContact());
        viewHolder.tv_phone.setText(orderReceiveInfo.getPhone());
        viewHolder.tv_address.setText(orderReceiveInfo.getAddress());
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ConfirmDialog dialog = new ConfirmDialog(context, "提示", "确定删除该条订单接收信息？", true);
                dialog.setConfirmListener(new ConfirmDialog.ConfirmListener() {
                    @Override
                    public void confirm() {
                        if (listener != null) {
                            listener.onItemDelete(position);
                            notifyItemRemoved(position);
                        }
                    }
                });
                dialog.show();


                return true;
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemSelect(orderReceiveInfo);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return orderReceiveInfoList.size();
    }

    class OrderReceiveInfoViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_contact;
        private TextView tv_phone;
        private TextView tv_address;

        public OrderReceiveInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_contact = itemView.findViewById(R.id.tv_contact);
            tv_phone = itemView.findViewById(R.id.tv_phone);
            tv_address = itemView.findViewById(R.id.tv_address);
        }
    }

    public interface InteractionListener {
        void onItemDelete(int position);

        void onItemSelect(OrderReceiveInfo info);
    }
}
